package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.AnswerActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.UserAskActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AskBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.DeleteMyAskModel;

public class MyAskListAdapter extends BaseAdapter<AskBean> {

    public MyAskListAdapter(Context context, List<AskBean> items, int layoutResourse) {
        super(context, items, layoutResourse);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        MyAskListViewHolder viewHolder = new MyAskListViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyAskListViewHolder viewHolder=(MyAskListViewHolder) holder;
        AskBean askBean=items.get(position);
        final String title = askBean.getAsk_title();


        viewHolder.myaskitem_content.setText(title);
        final String id = askBean.getAsk_id();
        final String count = askBean.getAnswercount();
        viewHolder.myaskitem_num.setText(count+"条回答");
        viewHolder.myaskitem_content.setText(title);
        viewHolder.myaskitem_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog dlg = new AlertDialog.Builder(context)//builder创建对话框
                        .setIcon(R.mipmap.kpl)//显示头像
                        .setTitle("温馨提示")//显示标题
                        .setMessage("你确定要删除吗？")//显示类容
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                DeleteMyAskModel deleteMyAskModel = new DeleteMyAskModel();
                                deleteMyAskModel.deleteMyAsk(id, new TListener<SuccessBean>() {
                                    @Override
                                    public void onResponse(SuccessBean successBean) {
                                        if (successBean.getSuccess().equals("1")){
                                            if(UserAskActivity.class.isInstance(context)){
                                                UserAskActivity activity = (UserAskActivity)context;
                                                activity.finish();}
                                            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                                        }
                                        Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                                    }
                                    @Override
                                    public void onFail(String msg) {
                                        Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        })//按钮，null可以选择点击后要执行的事情
                        .create();//创建对话框
                dlg.show();

            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AnswerActivity.class);
                intent.putExtra("ask_id",id);
                context.startActivity(intent);
            }
        });
    }
}
